package com.yjd.api.user;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServletUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.UUID;

/**
 * 图形验证码
 * Created by yangchangyan on 2017/10/15.
 */
@Controller("com.yjd.api.user.ImageCodeControl")
@Scope("prototype")
@RequestMapping("/image/code")
public class ImageCodeControl extends BaseControl {

    /**
     * 获取图片验证码信息
     *
     * @throws Exception
     */
    @RequestMapping("/id")
    public void index() throws Exception {
        String imageCodeUid = UUID.randomUUID().toString();
        this.successData(FrameUtil.newHashMap("imageCodeUid", imageCodeUid, "imgUrl", ServletUtil.getRootPath() + "/image/code/img/" + imageCodeUid));
    }

    /**
     * 获取图片
     *
     * @throws Exception
     */
    @RequestMapping("/img/{imageCodeUid}")
    public void img(@PathVariable("imageCodeUid") String imageCodeUid) throws Exception {
        ServletUtil.generateCode(this.request, this.response, 5, imageCodeUid);
    }
}
